package com.pioneer.beans;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import org.jdom.Element;
import org.jdom.Document;
import com.pioneer.dao.*;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class Job {

	private String quoteNum;	
	private Date jobDate = new Date();
	private String jobName;
	private String address;
	private String city;
	private String state;
	private String zip;
	private String salesRep;
	private String regSalesMan;
	private int qty;
	private String type;
	private String supplierName;
	private String supplierContact;
	private String contractorName;
	private String contractorContact;
	private String builderName;
	private String status;
	private Date shipDate;
	private String terms;
	private String notes;
	private List jobItems;
	private Date expirationDate;
	
	private static DateFormat formatter=new SimpleDateFormat("MM/dd/yyyy");
	
	public Job() {		
	}
	
	private static Date parseDateString(String dateStr)
	throws ParseException{
		if ((dateStr!=null) && (dateStr.length()>0)) {
			return formatter.parse(dateStr); 
		}
		return null;
	}
	
	private static String formatDateString(Date date) {
		if (date!=null) return formatter.format(date);
		return "";
	}
	
    public Job(String quoteNum,	String jobDateString, String jobName, String address,
		String city, String state, String zip, String salesRep, String regSalesMan, String qtyString,
		String type, String supplierName, String supplierContact, String contractorName,
		String contractorContact, String builderName,
		String status, String shipDateString, String terms, String notes, String expirationDateString)
	throws ParseException,NumberFormatException {
		this(quoteNum,	parseDateString(jobDateString), jobName, address,
		city, state, zip, salesRep, regSalesMan, new Integer(qtyString).intValue(),
		type, supplierName, supplierContact, contractorName,
		contractorContact,	builderName, status, parseDateString(shipDateString), terms, notes,
		parseDateString(expirationDateString));
	}
	
    public Job(String quoteNum,	Date jobDate, String jobName, String address,
		String city, String state, String zip, String salesRep, String regSalesMan, int qty,
		String type, String supplierName, String supplierContact, String contractorName,
		String contractorContact,String builderName,
		String status, Date shipDate, String terms, String notes, Date expirationDate){		
		this.setQuoteNum(quoteNum);
		this.jobDate = jobDate;
		this.setJobName(jobName);
		this.setAddress(address);
		this.setCity(city);
		this.setState(state);
		this.setZip(zip);
		this.setSalesRep(salesRep);
		this.setRegSalesMan(regSalesMan);
		this.qty = qty;
		this.setType(type);
		this.setSupplierName(supplierName);
		this.setSupplierContact(supplierContact);
		this.setContractorName(contractorName);
		this.setContractorContact(contractorContact);
		this.setBuilderName(builderName);
		this.setStatus(status);
		this.shipDate = shipDate;
		this.setTerms(terms);
		this.setNotes(notes);		
		this.expirationDate= expirationDate;
    }	

	public Document getDocRep(ContactDAO contactDAO, JobDAO jobDAO)
	throws DataAccessException{
		Element rootElement=new Element("Job");
		rootElement.addContent(new Element("QuoteNum").addContent(quoteNum));
		rootElement.addContent(new Element("JobName").addContent(jobName));
		rootElement.addContent(new Element("Date").addContent(getJobDateString()));
		rootElement.addContent(new Element("ShipDate").addContent(getShipDateString()));
		rootElement.addContent(new Element("ExpirationDate").addContent(getExpirationDateString()));
		rootElement.addContent(new Element("SalesRep").addContent(salesRep));
		rootElement.addContent(new Element("RegSalesMan").addContent(regSalesMan));		
		rootElement.addContent(new Element("Status").addContent(status));
		rootElement.addContent(new Element("Qty").addContent(getQtyString()));
		rootElement.addContent(new Element("Type").addContent(type));
		rootElement.addContent(new Element("Address").addContent(address));
		rootElement.addContent(new Element("City").addContent(city));
		rootElement.addContent(new Element("State").addContent(state));
		rootElement.addContent(new Element("Zip").addContent(zip));
		rootElement.addContent(new Element("Terms").addContent(terms));
		rootElement.addContent(new Element("Notes").addContent(notes));
		rootElement.addContent(new Element("ContractorName").addContent(contractorName));
		rootElement.addContent(new Element("ContractorContact").addContent(contractorContact));
		ContactBean contractor = contactDAO.findContact(contractorName);
		rootElement.addContent(new Element("ContractorPhone").addContent(contractor.getBusinessPhone()));
		rootElement.addContent(new Element("ContractorFax").addContent(contractor.getBusinessFax()));
		rootElement.addContent(new Element("ContractorAddress").addContent(contractor.getBusinessAddress()));
		rootElement.addContent(new Element("ContractorCity").addContent(contractor.getBusinessCity()));
		rootElement.addContent(new Element("ContractorState").addContent(contractor.getBusinessState()));
		rootElement.addContent(new Element("ContractorZip").addContent(contractor.getBusinessZip()));		
		rootElement.addContent(new Element("SupplierName").addContent(supplierName));
		rootElement.addContent(new Element("SupplierContact").addContent(supplierContact));
		ContactBean supplier = contactDAO.findContact(supplierName);
		rootElement.addContent(new Element("SupplierPhone").addContent(supplier.getBusinessPhone()));
		rootElement.addContent(new Element("SupplierFax").addContent(supplier.getBusinessFax()));		
		rootElement.addContent(new Element("SupplierAddress").addContent(supplier.getBusinessAddress()));
		rootElement.addContent(new Element("SupplierCity").addContent(supplier.getBusinessCity()));
		rootElement.addContent(new Element("SupplierState").addContent(supplier.getBusinessState()));
		rootElement.addContent(new Element("SupplierZip").addContent(supplier.getBusinessZip()));		
		rootElement.addContent(new Element("BuilderName").addContent(builderName));
		ContactBean builder = contactDAO.findContact(builderName);
		rootElement.addContent(new Element("BuilderCity").addContent(builder.getBusinessCity()));
		rootElement.addContent(new Element("BuilderState").addContent(builder.getBusinessState()));
		Element jobItemListElement=new Element("ItemList");
		double total = 0;
		for (int jobItemCount=0;jobItemCount<jobItems.size();jobItemCount++) {
			Element jobItemElement=new Element("JobItem");
			JobItem jobItem=(JobItem)jobItems.get(jobItemCount);
			jobItemElement.addContent(new Element("ItemNum").addContent(jobItem.getItemNum()));
			jobItemElement.addContent(new Element("ItemQty").addContent(jobItem.getQtyString()));			
			jobItemElement.addContent(new Element("ItemDescription").addContent(jobItem.getItemDesc()));
			Item item = jobDAO.getItem(jobItem.getItemNum());
			if (item!=null) {				
				jobItemListElement.addContent(jobItemElement);
				jobItemElement.addContent(new Element("ItemPrice").addContent(jobItem.getPriceString()));
				double amt = jobItem.getPrice()*jobItem.getQty();
				total = total+amt;
				jobItemElement.addContent(new Element("Amount").addContent(Double.toString(amt)));
			}
		}
		rootElement.addContent(jobItemListElement);
		rootElement.addContent(new Element("Total").addContent(Double.toString(total)));
		return new Document(rootElement);
	}


	public String getQuoteNum() {
		return this.quoteNum;
	}
	
	public String getJobDateString(){
		return formatDateString(jobDate);		
	}
	
	public Date getJobDate() {
		return jobDate;
	}
	
	public String getJobName() {
		return this.jobName;
	}
	
	public String getAddress() {
		return this.address;
	}
	
	public String getCity() {
		return this.city;
	}
	
	public String getState() {
		return this.state;
	}
	
	public String getZip() {
		return this.zip;
	}
	
	public String getSalesRep() {
		return this.salesRep;
	}
	
	public String getRegSalesMan() {
		return this.regSalesMan;
	}
	
	public String getQtyString() {
		return Integer.toString(this.qty);
	}
	
	public int getQty() {
		return qty;
	}
	
	public String getType() {
		return this.type;
	}
	
	public String getSupplierName() {
		return this.supplierName;
	}
	
	public String getContractorName() {
		return this.contractorName;
	}
	
	public String getSupplierContact() {
		return this.supplierContact;
	}
	
	public String getContractorContact() {
		return this.contractorContact;
	}
	
	public String getBuilderName() {
		return this.builderName;
	}
	public String getStatus() {
		return this.status;
	}
	
	public String getShipDateString(){
		return formatDateString(shipDate);
	}
	
	public Date getShipDate() {
		return shipDate;
	}
	
	public String getTerms() {
		return this.terms;
	}
	
	public String getNotes() {
		return this.notes;
	}
	
	public List getJobItems() {
		return this.jobItems;
	}
	
	public String getExpirationDateString(){
		return formatDateString(expirationDate);
	}
	
	public Date getExpirationDate() {
		return expirationDate;
	}
	
	// setters
	
	private String toUpper(String lower) {
		if (lower!=null) return lower.toUpperCase();
		return lower;
	}
	
	public void setQuoteNum(String quoteNum){
		this.quoteNum = toUpper(quoteNum);
	}
	
	public void setJobDate(String jobDateString)
	throws ParseException{
		this.jobDate = parseDateString(jobDateString);
	}
	
	public void setJobName(String jobName){
		this.jobName = toUpper(jobName);
	}
	
	public void setAddress(String address){
		this.address = toUpper(address);
	}
	
	public void setCity(String city){
		this.city = toUpper(city);
	}
	
	public void setState(String state){
		this.state = toUpper(state);
	}
	
	public void setZip(String zip){
		this.zip = zip;
	}
	
	public void setSalesRep(String salesRep){
		this.salesRep = salesRep;
	}
	
	public void setRegSalesMan(String regSalesMan){
		this.regSalesMan = regSalesMan;
	}
	
	public void setQty(String qtyString)
	throws NumberFormatException{
		if (qtyString==null) throw new NumberFormatException("Cannot pass null to int");
		this.qty=new Integer(qtyString).intValue();
	}
	
	public void setType(String type){
		this.type = type;
	}
	
	public void setSupplierName(String supplierName){
		this.supplierName = supplierName;
	}
	
	public void setSupplierContact(String supplierContact){
		this.supplierContact = supplierContact;
	}
	
	public void setContractorName(String contractorName){
		this.contractorName = contractorName;
	}
	
	public void setContractorContact(String contractorContact){
		this.contractorContact = contractorContact;
	}
	
	public void setBuilderName(String builderName){
		this.builderName = builderName;
	}
	public void setStatus(String status){
		this.status = status;
	}
	
	public void setShipDate(String shipDateString)
	throws ParseException{
		this.shipDate = parseDateString(shipDateString);
	}
	
	public void setTerms(String terms){
		this.terms = toUpper(terms);
	}
	
	public void setNotes(String notes){
		this.notes = toUpper(notes);
	}
	
	public void setJobItems(List jobItems) {
		this.jobItems = jobItems;
	}
	
	public void setExpirationDate(String expirationDateString)
	throws ParseException{
		this.expirationDate = parseDateString(expirationDateString);
	}

}